﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FinalProject.DAL.Mapping;
using FinalProject.DAL.Model;
using System.Collections;

namespace FinalProject.BL
{
    public class ItemBL : BaseBL<Item>
    {
        public static string GetWarehouseCodeById(Guid itemId)
        {
            try
            {
                string result = string.Empty;

                var query = from item in dalContext.Items
                            join warehouse in dalContext.Warehouses on item.WarehouseId equals warehouse.Id
                            where item.Id == itemId
                            select new { warehouse.Name };
                if (query.SingleOrDefault() != null)
                {
                    result = query.SingleOrDefault().Name;
                }
                return result;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static ArrayList GetAllDataForEditGrid(Guid itemId)
        {
            ArrayList result = new ArrayList();
            try
            {
                var query = from item in dalContext.Items
                            join warehouse in dalContext.Warehouses on item.WarehouseId equals warehouse.Id
                            where item.Id == itemId
                            select new
                            {
                                WarehouseName = warehouse.Name,
                                WarehouseId = warehouse.Id,
                                Price = item.Price
                            };
                if (query.SingleOrDefault() != null)
                {
                    result.Add(query.SingleOrDefault().WarehouseName);
                    result.Add(query.SingleOrDefault().WarehouseId);
                    result.Add(query.SingleOrDefault().Price);
                }
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static bool IsInStock(Guid itemId, int quantity)
        {
            try
            {
                if (itemId == Guid.Empty) return true;

                Item item = LoadById(itemId);
                return quantity <= item.StockNumber;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
